mod hello_world;
mod mirror_body_json;
mod mirror_body_string;
mod mirror_custom_header;
mod mirror_user_agent;
mod read_middleware_custom_header;
mod set_middleware_custom_header;
mod always_errors;
mod returns_201;
mod get_json;
mod validate_data;

use hello_world::hello_world;
use mirror_body_string::mirror_body_string;
use mirror_body_json::mirror_body_json;
use mirror_user_agent::mirror_user_agent;
use mirror_custom_header::mirror_custom_header;
use read_middleware_custom_header::read_middleware_custom_header;
// use set_middleware_custom_header::set_middleware_custom_header;
use always_errors::always_errors;
use returns_201::returns_201;
use get_json::get_json;
use validate_data::validate_data;

use axum::{extract::FromRef, routing::get, Router, middleware};

use axum::http::Method;
use axum::routing::post;
use tower_http::cors::{Any, CorsLayer};

#[derive(Clone, FromRef)]
pub struct SharedData {
    pub message: String,
}

pub fn create_routes() -> Router {
    let cors = CorsLayer::new()
    .allow_methods([Method::GET, Method::POST])
    .allow_origin(Any);

    let shared_data = SharedData {
        message: "Hello from shared data, I am a State now".to_owned(),
    };


    Router::new()
        .route("/read_middleware_custom_header", get(read_middleware_custom_header))
        // .route_layer(middleware::from_fn(set_middleware_custom_header))
        .route("/hello_world", get(hello_world))
        .route("/mirror_body_string", post(mirror_body_string))
        .route("/mirror_body_json", post(mirror_body_json))
        .route("/mirror_user_agent", get(mirror_user_agent))
        .route("/mirror_custom_header", get(mirror_custom_header))
        .with_state(shared_data)
        .layer(cors)
        .route("/always_errors", get(always_errors))
        .route("/returns_201", get(returns_201))
        .route("/get_json", get(get_json))
        .route("/validate_data", post(validate_data))

}
